package org.jeecg.modules.flow.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.flow.entity.FlowAudit;
import org.jeecg.modules.flow.service.IFlowAuditService;
import org.jeecg.modules.flow.vo.FlowActAuditVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Set;

/**
 * @Description: 流程审批意见
 * @Author: zzq
 * @Date: 2021-11-08
 * @Version: V1.0
 */
@Api(tags = "流程审批意见")
@RestController
@RequestMapping("/flow/audit")
@Slf4j
public class FlowAuditController extends JeecgController<FlowAudit, IFlowAuditService> {
	@Autowired
	private IFlowAuditService flowAuditService;

	/**
	 * 分页列表查询
	 *
	 * @param flowAudit
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiOperation(value = "flow_audit-分页列表查询", notes = "flow_audit-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(FlowAudit flowAudit,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<FlowAudit> queryWrapper = QueryGenerator.initQueryWrapper(flowAudit, req.getParameterMap());
        Page<FlowAudit> page = new Page<FlowAudit>(pageNo, pageSize);
        IPage<FlowAudit> pageList = flowAuditService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    @ApiOperation("根据流程实例id查询审核意见列表")
    @GetMapping("/queryAuditListByInstanceId")
    public Result<List<FlowAudit>> queryAuditListByInstanceId(@RequestParam String processInstanceId) {
		List<FlowAudit> flowAudits = flowAuditService.listByInstanceId(processInstanceId);
		return Result.OK(flowAudits);
	}

	@ApiOperation("根据流程任务id查询审核意见")
	@GetMapping("/queryAuditByTaskId")
	public Result<FlowAudit> queryAuditByTaskId(@RequestParam String taskId) {
		FlowAudit flowAudit = flowAuditService.getByTaskId(taskId);
		return Result.OK(flowAudit);
	}

	@ApiOperation("根据流程任务ids查询审核意见列表")
	@PostMapping("/queryAuditByTaskId")
	public Result<List<FlowActAuditVo>> queryActAuditByTaskIds(@RequestBody Set<String> taskIds) {
		List<FlowActAuditVo> flowAuditVo = flowAuditService.getByTaskIds(taskIds);
		return Result.OK(flowAuditVo);
	}


	/**
	 * 添加
	 *
	 * @param flowAudit
	 * @return
	 */

	@ApiOperation(value = "flow_audit-添加", notes = "flow_audit-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody FlowAudit flowAudit) {
		flowAuditService.save(flowAudit);
		return Result.OK("添加成功！");
	}

	/**
	 * 编辑
	 *
	 * @param flowAudit
	 * @return
	 */
	@ApiOperation(value = "flow_audit-编辑", notes = "flow_audit-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody FlowAudit flowAudit) {
		flowAuditService.updateById(flowAudit);
		return Result.OK("编辑成功!");
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "flow_audit-通过id删除", notes = "flow_audit-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
		flowAuditService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "flow_audit-通过id查询", notes = "flow_audit-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
		FlowAudit flowAudit = flowAuditService.getById(id);
		if (flowAudit == null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(flowAudit);
	}

}
